package com.xthh._function;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.xt.xthh.R;


public class HH_DialogUtil {

    private static AlertDialog dialog;

//回调接口
    public interface ResultCallBackSingle {
        public void callback(int flag);
    }
    public interface ResultCallBack {
        public void callback(boolean flag);
    }
    public interface ResultCallBackMulti {
        public void callback(boolean[] selected);
    }
    public interface ResultCallBackInput {
        public void callback(String info);
    }

    public static void tipsDialog(Context context, String str, final ResultCallBack resultCallBack) {

        dialog = new AlertDialog.Builder(context)
                .setTitle(context.getString(R.string.notifyTitle))
                .setMessage(str)
                .setPositiveButton(context.getString(R.string.strID_181), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        if (resultCallBack != null) {
                            resultCallBack.callback(true);
                        }
                    }
                }).create();


        dialog.setCanceledOnTouchOutside(false);//dialog弹出后会点击屏幕，dialog不消失；点击物理返回键dialog消失
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                //屏蔽搜索键
                //默认返回 false
                return keyCode == KeyEvent.KEYCODE_SEARCH;
            }
        });
        dialog.show();
    }

    public static void tipsDialog(Context context, String str) {

        dialog = new AlertDialog.Builder(context)
                .setTitle(context.getString(R.string.notifyTitle))
                .setMessage(str)
                .setPositiveButton(context.getString(R.string.strID_181), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                }).create();


        dialog.setCanceledOnTouchOutside(false);//dialog弹出后会点击屏幕，dialog不消失；点击物理返回键dialog消失
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                //屏蔽搜索键
                //默认返回 false
                return keyCode == KeyEvent.KEYCODE_SEARCH;
            }
        });
        dialog.show();
    }

    public static void showDialog(Context context, String str, final ResultCallBack resultCallBack) {

        dialog = new AlertDialog.Builder(context)
                .setTitle(context.getString(R.string.notifyTitle))
                .setMessage(str)
                .setPositiveButton(context.getString(R.string.strID_181), new DialogInterface.OnClickListener() {
                    @Override

                    public void onClick(DialogInterface dialog, int which) {

                        if (resultCallBack != null) {
                            resultCallBack.callback(true);
                        }
                    }
                })

                .setNegativeButton(context.getText(R.string.cancel), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (resultCallBack != null) {
                            resultCallBack.callback(false);
                        }
                    }
                }).create();


        dialog.setCanceledOnTouchOutside(false);//dialog弹出后会点击屏幕，dialog不消失；点击物理返回键dialog消失
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {

                if (keyCode == KeyEvent.KEYCODE_SEARCH) {//屏蔽搜索键
                    return true;
                } else {
                    return false; //默认返回 false
                }
            }
        });
        dialog.show();
    }

    // 单选
    public static void showDialogSingle(Context context, String title, String okStr, String cancelStr, String[] selectStr, int defaultSelect, final ResultCallBackSingle resultCallBack) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        int[] nCurrentSelect = new int[2];
        nCurrentSelect[0] = defaultSelect;
        builder.setTitle(title);
        builder.setSingleChoiceItems(selectStr, defaultSelect, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                nCurrentSelect[0] = which;
            }
        });

        DialogInterface.OnClickListener btnListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int which) {
                if (resultCallBack != null) {
                    resultCallBack.callback(nCurrentSelect[0]);
                }
            }
        };

        builder.setPositiveButton(okStr, btnListener);
        builder.setNegativeButton(cancelStr, null);
        AlertDialog dialog = builder.create();
        dialog.show();
        dialog.setCanceledOnTouchOutside(true);// dialog弹出后，点击界面其他部分dialog消失
    }


    // 多选，返回多个的结果
    public static void showDialogMulti(Context context, String title, String[] selectStr , boolean[] selected, final ResultCallBackMulti resultCallBack) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title);
        DialogInterface.OnMultiChoiceClickListener mutiListener = new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int which, boolean isChecked) {
                selected[which] = isChecked;
            }
        };
        builder.setMultiChoiceItems(selectStr, selected, mutiListener);
        DialogInterface.OnClickListener btnListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int which) {
                if (resultCallBack != null) {
                    resultCallBack.callback(selected);
                }
             }
        };
        builder.setNegativeButton("取消", null);
        builder.setPositiveButton("确定", btnListener);
        AlertDialog dialog = builder.create();
        dialog.show();
        dialog.setCanceledOnTouchOutside(true);// dialog弹出后，点击界面其他部分dialog消失
    }

    // 带编辑框的
    public static void showCustomInputDialog(Context context,
                                             String title,                     // 对话框的标题
                                             String caption, String content,   // 输入框的标题及内容
                                             String ok, String cancel,         // 确认和取消按钮
                                             final ResultCallBackInput resultCallBack) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View customDialogLayout = inflater.inflate(R.layout.custom_dialog_layout_info_input, null);
        final TextView textview_caption = (TextView) customDialogLayout.findViewById(R.id.textview_caption);
        textview_caption.setText(caption);

        final EditText edittext_content = (EditText) customDialogLayout.findViewById(R.id.edittext_content);
        edittext_content.setText(content);

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title);
        // 设置正向操作按钮及其监听器
        builder.setPositiveButton(ok, (dialogInterface, x) -> {
            String inputText_name = edittext_content.getText().toString();
            if (resultCallBack != null) {
                resultCallBack.callback(inputText_name);
            }
        });

        // 可选设置取消按钮
        builder.setNegativeButton(cancel, null);
        // 构建对话框并将自定义视图添加进去
        AlertDialog dialog = builder.create();
        dialog.setView(customDialogLayout, 0, 0, 0, 0);

        // 显示对话框前请求获取焦点以激活软键盘
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        dialog.show();
    }
}